import tkinter as tk
import tkinter.ttk as ttk

from select import select

import common.common as common


class PageSettingsOutput(ttk.Frame):
    def __init__(self, parent):
        super().__init__(parent)
        self.text = None
        self.format_cb = None
        self.show_date_cb = None
        self.show_time_cb = None
        self.show_millis_cb = None
        self.show_rx_cb = None
        self.show_tx_cb = None
        self.auto_wrap_cb = None
        self.create_widgets()

    def create_widgets(self):
        row = 0
        title_font = ("TkDefaultFont", 10, "bold")
        title_label = ttk.Label(self, text="Output Settings", font=title_font)
        title_label.grid(row=row, column=0, columnspan=2, sticky="nsew", padx=2, pady=2)
        row += 1

        separator = ttk.Separator(self, orient="horizontal")
        separator.grid(row=row, column=0, columnspan=2, sticky="nsew", padx=2, pady=2)
        row += 1

        format_label = ttk.Label(self, text="Format")
        format_label.grid(row=row, column=0, sticky="nsew")
        format_cb = ttk.Combobox(self)
        common.setup_text_format_for_combobox(format_cb)

        format_cb.grid(row=row, column=1, sticky="nsew", padx=2, pady=2)
        self.format_cb = format_cb
        row += 1

        grid = ttk.Frame(self)
        grid.grid(row=row, column=0, columnspan=2, sticky="nsew")
        show_date = ttk.Checkbutton(grid, text="Date")
        show_date.grid(row=0, column=0, sticky="nsew", padx=2, pady=2)
        show_time = ttk.Checkbutton(grid, text="Time")
        show_time.grid(row=0, column=1, sticky="nsew", padx=2, pady=2)
        show_millis = ttk.Checkbutton(grid, text="Millis")
        show_millis.grid(row=0, column=2, sticky="nsew", padx=2, pady=2)
        show_rx = ttk.Checkbutton(grid, text="Rx")
        show_rx.grid(row=1, column=0, sticky="nsew", padx=2, pady=2)
        show_tx = ttk.Checkbutton(grid, text="Tx")
        show_tx.grid(row=1, column=1, sticky="nsew", padx=2, pady=2)
        auto_wrap = ttk.Checkbutton(grid, text="Wrap")
        auto_wrap.grid(row=1, column=2, sticky="nsew", padx=2, pady=2)
        auto_wrap.config(command=self.update_wrap_mode)
        grid.grid_columnconfigure(0, weight=1)
        grid.grid_columnconfigure(1, weight=1)
        grid.grid_columnconfigure(2, weight=1)
        self.show_date_cb = show_date
        self.show_time_cb = show_time
        self.show_millis_cb = show_millis
        self.show_rx_cb = show_rx
        self.show_tx_cb = show_tx
        self.show_tx_cb.state(["selected"])
        self.show_rx_cb.state(["selected"])
        self.auto_wrap_cb = auto_wrap
        row += 1

        row_layer = ttk.Frame(self)
        row_layer.grid(row=row, column=0, columnspan=2, sticky="nsew", padx=2, pady=2)
        settings_button = ttk.Button(row_layer, text="Settings")
        settings_button.grid(row=0, column=0, sticky="nsew")
        clear_button = ttk.Button(row_layer, text="Clear")
        clear_button.grid(row=0, column=1, sticky="nsew")
        clear_button.config(command=self.clear_text)
        row_layer.grid_columnconfigure(0, weight=1)
        row_layer.grid_columnconfigure(1, weight=1)
        row += 1

        self.grid_columnconfigure(1, weight=1)

    def update_wrap_mode(self):
        if self.text is None or self.auto_wrap_cb is None:
            raise ValueError("Text widget is not initialized")
        wrap = self.auto_wrap_cb.instate(["selected"])
        self.text.config(wrap=tk.WORD if wrap else tk.NONE)

    def clear_text(self):
        if self.text is None:
            raise ValueError("Text widget is not initialized")
        self.text.config(state=tk.NORMAL)
        self.text.delete("1.0", tk.END)
        self.text.config(state=tk.DISABLED)

    def parameters(self):
        ret = {
            "format": self.format_cb.get(),
            "show_date": self.show_date_cb.instate(["selected"]),
            "show_time": self.show_time_cb.instate(["selected"]),
            "show_millis": self.show_millis_cb.instate(["selected"]),
            "show_rx": self.show_rx_cb.instate(["selected"]),
            "show_tx": self.show_tx_cb.instate(["selected"]),
        }

        return ret
